﻿@{
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
@section ScriptAndCss{
    <link href="@Url.Content("~/Content/css/plan.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/json3.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/plan.js")" type="text/javascript"></script>
}
<div id="AnnouncementPanel" class="padding5" style="height:100%;">
    <div style="height: 50%;">
        <table id="AnnouncementList" class="" title="电子公告<span style='color:red;font-size:12px;font-weight:normal;'>(双击进行修改)</span>"
            fitcolumns="true" singleselect="true" pagination="true" fit="true">
            <thead>
                <tr>
                    <th field="ID" width="140" sortable="true">编号</th>
                    <th field="Title" width="140" sortable="true">标题</th>
                    <th field="Content" width="140" sortable="true" hidden="true"></th>
                    <th field="BeginTime" formatter="DateFormate" width="100" sortable="true">开始时间</th>
                    <th field="EndTime" formatter="DateFormate" width="100" sortable="true">失效时间</th>
                    <th field="CreateTime" formatter="DateFormate" width="100" sortable="true">创建时间</th>
                    <th field="CreateByName" width="100" sortable="true">发布人</th>
                    <th field="Ope" width="60" formatter="OpeRender">操作</th>
                </tr>
            </thead>
        </table>
    </div>

    <div class="outer_border" id="bulletinDetail" style="padding: 5px 0; margin: 5px 0; border: solid 1px black;">
        <input type="hidden" data-source="ID" value="" />
        <div class="parameter">
            <div class="parameter_name"><span class="required">*</span>开始时间：</div>
            <div class="parameter_input">
                <input type="text" id="bulletinStartDate" class="easyui-datebox startDate" data-source="BeginTime" data-required-info="请选择开始时间！" />
            </div>
        </div>
        <div class="parameter">
            <div class="parameter_name"><span class="required">*</span>失效时间：</div>
            <div class="parameter_input">
                <input type="text" id="bulletinEndDate" class="easyui-datebox endDate" data-source="EndTime" data-required-info="请选择失效时间！" />
            </div>
        </div>
        <div class="parameter one_line">
            <div class="parameter_name"><span class="required">*</span>标题：</div>
            <div class="parameter_input" style="width: 660px;">
                <input type="text" id="bulletinTitle" onkeyDown="return checkTextAreaMaxLength(this,event,'50');" maxlength="50" data-source="Title" data-required-info="请填写公告标题！" />
            </div>
        </div>
        <div class="parameter one_line">
            <div class="parameter_name"><span class="required">*</span>内容：</div>
            <div class="parameter_input" style="width: 660px;">
                <textarea rows="5" id="bulletinContent" maxlength="1000" data-source="Content" data-required-info="请填写公告内容！"></textarea>
            </div>
        </div>
        <div id="createButton">
            <input type="button" value="添加电子公告" onclick="publish()" />
        </div>
        <div id="updateButton" style="display: none;">
            <input type="button" value="修改" onclick="update()" />
            <input type="button" value="取消" onclick="cancel()" />
        </div>
    </div>
</div>

<script type="text/javascript">
    var DateFormate = function (value, row, index) {
        if (value) {
            return value.DateStringConvert('yyyy-MM-dd');
        }
    };

    var OpeRender = function () {
        return '<a class="deleteRow">删除</a>';
    };

    var setDetailData = function (detail) {

        $.each($('#bulletinDetail').find('[data-source]'), function () {
            var propName = $(this).attr('data-source');
            if ($(this).hasClass('easyui-datebox')) {
                $(this).datebox('setValue', detail[propName].DateStringConvert());
            } else {
                $(this).val(detail[propName]);
            }
        });
    };

    var getDetailDate = function () {
        var detail = {};
        $.each($('#bulletinDetail').find('[data-source]'), function () {
            var propName = $(this).attr('data-source');
            if ($(this).hasClass('easyui-datebox')) {
                detail[propName] = $(this).datebox('getValue');
            } else {
                detail[propName] = $(this).val();
            }
        });

        return detail;
    };

    var clearDetailData = function () {
        $.each($('#bulletinDetail').find('[data-source]'), function () {

            if ($(this).hasClass('easyui-datebox')) {
                $(this).datebox('setValue', new Date().formate());
            } else {
                $(this).val('');
            }
        });
    };

    var validata = function () {
        var errors = [];

        //非空验证
        $.each($('#bulletinDetail').find('[data-required-info]'), function () {
            var errorInfo = $(this).attr('data-required-info');
            var val = '';
            if ($(this).hasClass('easyui-datebox')) {
                val = $(this).datebox('getValue');
            } else {
                val = $(this).val();
            }

            if (val == "") {
                errors.push(errorInfo);
            }
        });

        var startDate = $('#bulletinStartDate').datebox('getValue');
        var endDate = $('#bulletinEndDate').datebox('getValue');
        var today = new Date().formate();

        //开始时间<=今天
        if (startDate) {
            if (!dateCompare("AnnouncementPanel", startDate, today, true)) {
                errors.push("开始时间必须小于等于今天!");
            }
        }

        //结束时间>=今天
        if (endDate) {
            if (!dateCompare("AnnouncementPanel", today, endDate, true)) {
                errors.push("失效时间必须大于等于今天!");
            }
        }

        //if (startDate && endDate) {
        //    if (!dateCompare("AnnouncementPanel", startDate, endDate, true)) {
        //        errors.push("失效时间必须大于等于开始时间!");
        //    }
        //}

        if (errors.length != 0) {
            var content = '<ul class="warnTip">';

            $.each(errors, function (index, _e) {
                content += "<li>" + _e + "</li>"
            });

            content += "</ul>";
            $.messager.alert('提醒', content, 'warning');

            return false;
        }

        return true;
    };

    var postAnnouncement = function (data) {
        $.post('/PlanManagement/AddAnnouncement', data, function (res) {
            if (res == "OK") {
                $.messager.alert('提示', '保存成功!', 'info');
                $('#AnnouncementList').datagrid('reload');
                cancel();
            }
            if (res == "Error") {
                $.messager.alert('错误', '保存失败!', 'error');
            }
        });
    };

    var publish = function () {
        var data = getDetailDate();
        if (validata()) {
            $.messager.confirm('提醒', '确认发布这条公告吗？', function (r) {
                if (r) {
                    postAnnouncement(data);
                }
            });
        }
    };

    var update = function () {
        var data = getDetailDate();
        if (validata()) {
            $.messager.confirm('提醒', '确认保存修改这条公告吗？', function (r) {
                if (r) {
                    postAnnouncement(data);
                }
            });
        }
    };


    var cancel = function () {
        clearDetailData();
        $('#createButton').show();
        $('#updateButton').hide();
    };

    $(function () {

        $('.easyui-datebox').datebox({
            formatter: function (date) {
                return date.formate('yyyy-MM-dd')
            }
        });
        $('.validatebox-text').attr('disabled', true);

        $('.easyui-datebox').datebox('setValue', new Date().formate());

        $('#AnnouncementList').datagrid({
            url: '/PlanManagement/GetAnnouncementList',
            pageList: [10],
            pageSize: 10,
            pageNumber: 1,
            onLoadSuccess: function (data) {
                $('.datagrid-btable').find('td').each(function () {
                    $(this).attr('title', $(this).text());
                });
            },
            onClickCell: function (rowIndex, field, value) {
                if (field == "Ope") {
                    $.messager.confirm('提醒', '确认删除这条公告吗？', function (r) {
                        if (r) {
                            $.post('/PlanManagement/DeleteAnnouncement', {
                                ID: $('#AnnouncementList').datagrid('getData').rows[rowIndex]["ID"]
                            }, function (res) {
                                if (res == "OK") {
                                    $('#AnnouncementList').datagrid('reload');
                                }
                                if (res == "Error") {
                                    $.messager.alert('错误', '删除失败!', 'error');
                                }
                            });
                            $('#AnnouncementList').datagrid('deleteRow', rowIndex);
                        }
                    });
                }
            },
            //双击进行编辑
            onDblClickRow: function (rowIndex, rowData) {
                $('#updateButton').show();
                $('#createButton').hide();
                setDetailData(rowData);
            }

        });

        var p2 = $('#AnnouncementList').datagrid('getPager');
        $(p2).pagination({
            beforePageText: '第',
            afterPageText: '页&nbsp;共&nbsp;{pages}&nbsp;页',
            displayMsg: '当前显示{from} - {to}&nbsp;条记录&nbsp;共&nbsp;{total}&nbsp;条记录'

        });
    });
</script>
